From da4b8afba8afd888587d439c7f14ed06aa836d36 Mon Sep 17 00:00:00 2001 From: Gleb Kozyrev Date: Fri, 30 Oct 2015 13:31:14 +0200 Subject: [PATCH] Detect the line endings convention of Cargo.lock (CRLF or LF) and preserve it Fixes #2076 --- src/cargo/ops/lockfile.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/cargo/ops/lockfile.rs b/src/cargo/ops/lockfile.rs index 3a0ef553f..8f9489c14 100644 --- a/src/cargo/ops/lockfile.rs +++ b/src/cargo/ops/lockfile.rs @@ -70,6 +70,9 @@ pub fn write_lockfile(dst: &Path, resolve: &Resolve) -> CargoResult<()> { // Load the original lockfile if it exists. if let Ok(orig) = paths::read(dst) { + if has_crlf_line_endings(&orig) { + out = out.replace("\n", "\r\n"); + } if out == orig { // The lockfile contents haven't changed so don't rewrite it. // This is helpful on read-only filesystems. @@ -81,6 +84,15 @@ pub fn write_lockfile(dst: &Path, resolve: &Resolve) -> CargoResult<()> { Ok(()) } +fn has_crlf_line_endings(s: &str) -> bool { + // Only check the first line. + if let Some(lf) = s.find('\n') { + s[..lf].ends_with('\r') + } else { + false + } +} + fn emit_package(dep: &toml::Table, out: &mut String) { out.push_str(&format!("name = {}\n", lookup(dep, "name"))); out.push_str(&format!("version = {}\n", lookup(dep, "version"))); -- 2.30.2